<?php
    Class Attributes extends CI_Controller{
        var $data = array();

        function __construct()
        {
            parent::__construct();

            if(!$this->session->userdata('logged_in'))
                redirect(base_url('login'));

            $this->load->model('attributes_model');
            $this->load->model('group_attributes_model');

            $this->load->config('pages');
            $this->load->library('translate', array('lang' => 'en'));
        }

        /**
        * view_all
        * 
        * List all custom attributes
        * 
        * @param
        * 
        */
        public function view_all()
        {
            $this->data = array_merge($this->data, array(
                    'title' => 'Attributes',
                    'page' => 'Realestate',
                    'attributes' => $this->attributes_model->list_all()
                ));

            $cnt = count($this->data['attributes']);
            for($i=0;$i<$cnt;$i++){
                $this->data['attributes'][$i]['name'] = $this->translate->find_field($this->session->userdata('my_language'),$this->data['attributes'][$i]['name']);
            }


            $this->template->build('attributes/view_all',$this->data);
        }

        /**
        * view_new_attribute
        * 
        * Preview form for entering new attribute
        * 
        * @param $group_id
        * 
        */
        public function view_new_attribute($group_id)
        {
            $data['group_id'] = $group_id;
            $html = $this->load->view('attributes/new_attribute',$data,TRUE);
            echo $html;
        }

        /**
        * submit_new_attribute
        * 
        * Add new attribute to DB
        * 
        * @param $_POST
        * 
        */
        public function submit_new_attribute()
        {
            //parse language fields from input array[] in post
            $this->load->library('object_lib');
            $_POST = $this->object_lib->parse_post($_POST,config_item('wp_languages'));

            $validation_info = 'attribute';

            $this->form_validation->set_rules("attribute_type", "type", "dropdown_check[$validation_info]");
            $this->form_validation->set_rules("attribute_name", "name", "required");
            $this->form_validation->set_message("required","Please enter $validation_info <b>%s</b>!");
            if ($this->form_validation->run() == FALSE)
            {
                echo json_encode(array('action'=>FALSE, 'msg'=>validation_errors()));
                return;
            }

            $attribute_data = array(
                'type' => $_POST['attribute_type'],
                'name' => $_POST['attribute_name'],
                'required' => $this->input->post('attribute_required') ? 1 : 0,
                'custom' => 1
            );

            // insert into attributes
            $inser_id =  $this->attributes_model->insert($attribute_data);

            // insert into group_attrutes
            $group_attributes_data = array(
                'groups_ID' => $_POST['group_id'],
                'attributes_ID' => $inser_id,
                'active' => 0
            );
            $this->group_attributes_model->insert($group_attributes_data);

            echo json_encode(array('action'=>TRUE,'msg'=>'Successful.','id'=>$inser_id));
        }

        /**
        * delete
        * 
        * Delete attribute from DB
        * 
        * @param
        * 
        */
        public function delete($id)
        {
            $this->db->query("DELETE FROM group_attributes WHERE attributes_ID = $id");
            $this->attributes_model->delete($id); 
            redirect(base_url('attributes/view_all?post_type=realestate'));
        }

    }

?>
